from rest_framework.views import Response,APIView
from utils.jwt_tool import JwtTool
from utils.get_path import GetJW
from goods.models import Site


class AddSite(APIView):
    def post(self,request):
        token = request.headers.get('token')
        j = JwtTool()
        payload = j.check_token(token)
        details_gps = request.data.get('details_gps')
        g_username = request.data.get('g_username')
        g_mobile = request.data.get('g_mobile')
        g_sex = request.data.get('g_sex')
        title_id = request.data.get('title_id')
        g = GetJW()
        GPS_info = str(g.get_add_order(details_gps))
        # 经度
        j = GPS_info[0]
        # 维度
        w = GPS_info[1]
        Site.objects.create(g_username=g_username,
                            g_mobile=g_mobile,
                            g_sex=g_sex,
                            details_gps=details_gps,
                            title=title_id,
                            longitude=j,
                            dimensionality=w,
                            u_id=payload['id'])
        return Response({'msg':'添加收货地址成功!', 'code':200})


class GetSite(APIView):
    def get(self,request):
        # token = request.headers.get('token')
        # j = JwtTool()
        # payload = j.check_token(token)
        # site_all = Site.objects.filter(u_id=payload['u_id']).all()
        site_all = Site.objects.all()
        list5 = []
        for i in site_all:
            data5 = {
                'id':i.id,
                'g_username':i.g_username,
                'g_mobile':i.g_mobile,
                'g_sex':i.g_sex,
                'details_gps':i.details_gps,
                'title':i.title_type.get(str(i.title)),
                'longitude':i.longitude,
                'dimensionality':i.dimensionality,
                # 'u_id':payload['u_id']
            }
            list5.append(data5)
        return Response({'msg':'获取所有收货地址成功', 'code':200, 'data':list5})


class DelSite(APIView):
    def delete(self,request):
        s_id = request.data.get('id')
        Site.objects.filter(id=s_id).delete()
        return Response({'msg':'删除地址成功', 'code':200})


class UpdateSite(APIView):
    def put(self,request):
        id = request.data.get('id')
        g_username = request.data.get("g_username")
        g_mobile = request.data.get("g_mobile")
        g_sex = request.data.get("g_sex")
        details_gps = request.data.get("details_gps")
        longitude = request.data.get("longitude")
        dimensionality = request.data.get('dimensionality')
        Site.objects.filter(id=id).update(g_username=g_username,
                                          g_mobile=g_mobile,
                                          g_sex=g_sex,
                                          details_gps=details_gps,
                                          longitude=longitude,
                                          dimensionality=dimensionality)
        return Response({"msg":"修改地址信息  成功","code":200})